//
//  VulameView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/11.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class VulameView: NSView {
    
    var kTrackWidth:CGFloat = 18
    
    var kTrackHeight:CGFloat = 18
    
    var kVolamWidth:CGFloat {
        return self.kTrackWidth/20*5.2
    }
    
    var kVolamHeight:CGFloat {
       return  self.kTrackHeight/20*12
    }
    
    var value:CGFloat = 0.1 {
        didSet {
            self.trackTintView.snp.updateConstraints { (make) in
                make.height.equalTo(self.value*self.kVolamHeight)
            }
        }
    }
    
    var isMute:Bool = false {
        didSet {
            if self.isMute == true {
                self.trackImageV.image = NSImage.image(name:"icon_meeting_audio_mute")
                self.tintView.isHidden = true
            }else {
                self.trackImageV.image = NSImage.image(name:"icon_meeting_audio2")
                self.tintView.isHidden = false
            }
        }
    }
    
    
    //最小值0- 最大值1
    func setValue(value:CGFloat,animate:Bool) -> () {
        if animate == false {
            self.trackTintView.snp.updateConstraints { (make) in
                make.height.equalTo(value*kVolamHeight)
            }
        }else {
            NSAnimationContext.runAnimationGroup { (context) in
                context.duration = 1.0
                let timingFunc = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeOut)
                context.timingFunction = timingFunc
                
                //NSAnimationContext.runAnimationGroup { (context) in
                    //context.duration = 0.0
                    self.trackTintView.animator().snp.updateConstraints { (make) in
                        make.height.equalTo(value*self.kVolamHeight)
                    }
               // }
                self.layoutSubtreeIfNeeded()
                self.animator().layoutSubtreeIfNeeded()
            }
            
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(size:CGSize=CGSize.init(width: 18, height: 18)) {
        self.init()
        self.kTrackWidth = size.width
        self.kTrackHeight = size.height
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.addSubview(trackImageV)
        trackImageV.snp.makeConstraints { (make) in
            make.width.equalTo(self.kTrackWidth)
            make.height.equalTo(self.kTrackHeight)
            make.center.equalToSuperview()
        }
        
        self.maskLayer.frame = CGRect.init(x: 0, y: 0, width: kVolamWidth, height: kVolamHeight)
        self.tintView.wantsLayer = true
        self.tintView.layer?.mask = maskLayer
        
        self.addSubview(tintView)
        tintView.snp.makeConstraints { (make) in
            make.bottom.equalTo(trackImageV).offset(-kTrackHeight/20*5)
            make.width.equalTo(kVolamWidth)
            make.height.equalTo(kVolamHeight)
            make.centerX.equalToSuperview()
        }
        
        tintView.addSubview(trackTintView)
        trackTintView.snp.makeConstraints { (make) in
            make.height.equalTo(10)
            make.centerX.width.bottom.equalToSuperview()
        }
    }
    
    lazy var muteImageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"icon_meeting_audio_mute")!)
        return imageV
    }()
    
    lazy var trackImageV: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.image(name:"icon_meeting_audio2")!)
        return imageV
    }()
    
    lazy var maskLayer: CALayer = {
        let mask = CALayer()
        mask.backgroundColor = NSColor.black.cgColor
        // 這邊我們必須設定顏色，否則沒有效果
        mask.masksToBounds = true
        mask.cornerRadius = self.kVolamWidth/2
        return mask
    }()
    
    lazy var tintView: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var trackTintView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.green.cgColor
        return view
    }()
}
